from django.shortcuts import render

from goods.models import GoodsType, IndexGoodsBanner, IndexPromotionBanner, IndexTypeGoodsBanner


def create_index_html():
    # 获取商品分类数据
    types = GoodsType.objects.all()
    # 获取轮播图数据
    goods_banners = IndexGoodsBanner.objects.all().order_by('index')
    # 获取促销商品数据
    promotion_banners = IndexPromotionBanner.objects.all().order_by('index')

    # 给每个分类增加商品数据 标题、图片
    for type in types:
        type.title_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=0).order_by('index')
        type.image_banners = IndexTypeGoodsBanner.objects.filter(type=type, display_type=1).order_by('index')

    context = {
        'types': types,
        'goods_banners': goods_banners,
        'promotion_banners': promotion_banners

    }

    response = render(None, 'index.html', context)
    with open('./static/index.html','w') as f:
        f.write(response.content.decode())
